/* endephp extension for PHP */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_endephp.h"

#include "core.h"
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
	ZEND_PARSE_PARAMETERS_START(0, 0) \
	ZEND_PARSE_PARAMETERS_END()
#endif
PHP_FUNCTION(endephp_encode)
{
    zend_string *strg;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &strg) == FAILURE) {
        return;
    }

    /* If encoded */
    if (!memcmp(ZSTR_VAL(strg), endephp_header, sizeof(endephp_header))) {
        RETURN_FALSE;
    }

    char *t = emalloc(sizeof(endephp_header) + ZSTR_LEN(strg));
    memcpy(t, endephp_header, sizeof(endephp_header));
    if (ZSTR_LEN(strg) > 0) {
		endephp_encrypt(ZSTR_VAL(strg), ZSTR_LEN(strg));
        memcpy(t + sizeof(endephp_header), ZSTR_VAL(strg), ZSTR_LEN(strg));
    }

    RETURN_STR(zend_string_init(t, sizeof(endephp_header) + ZSTR_LEN(strg), 0));
}
/* don't open to outside
PHP_FUNCTION(endephp_decode)
{
    zend_string *strg;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &strg) == FAILURE) {
        return;
    }

    if (memcmp(ZSTR_VAL(strg), endephp_header, sizeof(endephp_header))) {
        RETURN_FALSE;
    }

    size_t len = ZSTR_LEN(strg) - sizeof(endephp_header);
    if (ZSTR_LEN(strg) > 0) {
        endephp_decrypt(ZSTR_VAL(strg) + sizeof(endephp_header), len);
    }

    RETURN_STR(zend_string_init(ZSTR_VAL(strg) + sizeof(endephp_header), len, 0));
}
*/

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(endephp)
{
    /* If you have INI entries, uncomment these lines
    REGISTER_INI_ENTRIES();
    */

    old_compile_file = zend_compile_file;
    zend_compile_file = cgi_compile_file;

    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(endephp)
{
    /* uncomment this line if you have INI entries
    UNREGISTER_INI_ENTRIES();
    */

    zend_compile_file = old_compile_file;

    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(endephp)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "endephp support", "enabled");
	php_info_print_table_row(2, "Version", PHP_ENDEPHP_VERSION);
	php_info_print_table_row(2, "Copyright By", "szsupernan");
	php_info_print_table_end();
}
/* }}} */

/* {{{ endephp_functions[]
 */
static const zend_function_entry endephp_functions[] = {
	PHP_FE(endephp_encode,		NULL)
	//PHP_FE(endephp_decode,		NULL)
	PHP_FE_END
};
/* }}} */

/* {{{ endephp_module_entry
 */
zend_module_entry endephp_module_entry = {
	STANDARD_MODULE_HEADER,
	"endephp",										/* Extension name */
	endephp_functions,						/* zend_function_entry */
	PHP_MINIT(endephp),					/* PHP_MINIT - Module initialization */
	PHP_MSHUTDOWN(endephp),		/* PHP_MSHUTDOWN - Module shutdown */
	NULL,												/* PHP_RINIT - Request initialization */
	NULL,												/* PHP_RSHUTDOWN - Request shutdown */
	PHP_MINFO(endephp),					/* PHP_MINFO - Module info */
	PHP_ENDEPHP_VERSION,				/* Version */
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_ENDEPHP
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(endephp)
#endif
